from .base import PermissionHandler
from django import forms
from stark.service.v1 import StarkConfig, StarkModelForm
from stark.service.v1 import get_choice_text, get_datetime_text


class RentBillConfig(PermissionHandler, StarkConfig):
    
    list_display = [
        "id",
        "contract_id",  # 外键Contract的关联ID
        "bill_month",
        "rent_amount",
        "water_usage",
        "electricity_usage",
        "gas_usage",
        "water_fee",
        "electricity_fee",
        "gas_fee",
        "total_fee",
        get_choice_text("支付状态", "paid_status"),
        get_datetime_text("支付日期", "paid_date")
    ]
    
    def get_model_form_class(self, is_add, request, pk, *args, **kwargs):
        # 定义模型表单类，动态排除指定字段
        class ModelForm(StarkModelForm):
            paid_date = forms.DateField(
                widget=forms.DateInput(
                    attrs={'type': 'date'},  # 使用HTML5日期选择器
                    format='%Y-%m-%d'  # 日期格式
                ),
                input_formats=['%Y-%m-%d'],  # 接受的输入格式
                label="支付日期"
            )
            
            class Meta:
                model = self.model_class  # 关联到当前模型
                # 方法1：明确指定需要显示的字段（推荐，更安全）
                # fields = ["house", "meter_type", "reading_date", "current_reading"]
                
                # 方法2：排除不需要的字段
                exclude = ["water_usage",
                           "electricity_usage",
                           "gas_usage",
                           "water_fee",
                           "electricity_fee",
                           "gas_fee",
                           "total_fee",
                           "paid_date"]
        
        return ModelForm
